package ${controllerPackage};

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeng.nicecommon.util.MapBuilder;
import com.zeng.nicecommon.util.ResponseUtil;
import ${voPackage}.${entityName}Vo;
import ${servicePackage}.${entityName}Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
<#if packageExtend??>
    <#list packageExtend as item>
        import ${item};
    </#list>
</#if>

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RequestMapping("/${entityName?uncap_first}")
@RestController
public class ${entityName}Controller {

    @Autowired
    ${entityName}Service ${entityName?uncap_first}Service;
<#if propertyExtend??>
    <#list propertyExtend as item>
        ${item}
    </#list>
</#if>

    @RequestMapping("/list")
    public Object list(Page page, ${entityName}Vo ${entityName?uncap_first}Vo) {
        List<${entityName}Vo> page1 = this.${entityName?uncap_first}Service.findPage(page, ${entityName?uncap_first}Vo);

        ArrayList<Map<String,Object>> objects = new ArrayList<>();
        page1.forEach(e->{
            Map<String, Object> build = MapBuilder.newBuilder()
                    .put("code", e.getCode())
                    .put("updateDate", e.getUpdateDate())
                    .put("insertDate", e.getInsertDate())
                    .put("id", e.getId())
    <#list table.rows as row>
                    .put("${row.name}", e.get${row.name?cap_first}())
    </#list>
                    .build();
            objects.add(build);

        });
        return ResponseUtil.success("操作成功")
                .addData("${entityName?uncap_first}", objects)
                .addData("page", page);

    }

    @RequestMapping("/detail")
        public Object detail(String code) {

        return ResponseUtil.success("操作成功").addData("${entityName?uncap_first}", ${entityName?uncap_first}Service.getOneByCode(code));

    }

    @RequestMapping("/add")
    public Object add(${entityName}Vo ${entityName?uncap_first}Vo) {

        return ResponseUtil.success("操作成功").addData("${entityName?uncap_first}", ${entityName?uncap_first}Service.addOne(${entityName?uncap_first}Vo));

    }

    @RequestMapping("/update")
    public Object update(${entityName}Vo ${entityName?uncap_first}Vo) {
        Integer update = this.${entityName?uncap_first}Service.update(${entityName?uncap_first}Vo);

        return ResponseUtil.success("操作成功")
                .addData("updateCount", update);
    }

    @RequestMapping("/delete")
    public Object delete(String code) {
        Integer delete = this.${entityName?uncap_first}Service.delete(code);
        return ResponseUtil.success("操作成功").addData("deleteCount", delete);
    }

    @RequestMapping("/batchDelete")
    public Object batchDelete(@RequestBody List<String> codes) {
        Integer integer = this.${entityName?uncap_first}Service.batchDelete(codes);

        return ResponseUtil.success("操作成功").addData("deleteCount", integer);
    }

<#if methodExtend??>
    <#list methodExtend as item>
        ${item}
    </#list>
</#if>
}

